﻿using KFC.MonAnF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KFC
{
    public partial class CapNhatGiaForm : Form
    {
        private MonAn _monAn;
        private Form1 _form;
        public CapNhatGiaForm(Form1 form)
        {
            InitializeComponent();
            _form = form;
        }

        private void CapNhatGiaForm_Load(object sender, EventArgs e)
        {
            //QuanLyMonAn qlma = QuanLyMonAn.Instance();
            List<string> dsMa = new List<string>();
            foreach (MonAn ma in QuanLyMonAn.dsMonAn)
            {
                dsMa.Add(ma.MaMonAn);
            }
            comboBoxMaMon.DataSource = dsMa;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButtonKhongDB.Checked == true)
            {
                int gia = int.Parse(textBoxGia.Text);
                int idx = comboBoxMaMon.SelectedIndex;
                MonAn ma = QuanLyMonAn.dsMonAn[idx];
                ma.DonGia = gia;
                QuanLyMonAn qlma = QuanLyMonAn.Instance();
                qlma.CapNhatGia(ma.MaMonAn, gia);
                MessageBox.Show("Đã cập nhật giá thành công");
            }
            if (radioButtonDB.Checked == true)
            {
                int gia = int.Parse(textBoxGia.Text);
                int idx = comboBoxMaMon.SelectedIndex;
                MonAn ma = QuanLyMonAn.dsMonAn[idx];
                
                
                int soTienChenhLech = (int)((float)(gia - ma.DonGia)/ma.SoLuong);
                
                ma.DonGia = gia;
                ma.ThayDoiGiaDongBo(soTienChenhLech);
                QuanLyMonAn qlma = QuanLyMonAn.Instance();
                qlma.CapNhatGia(ma.MaMonAn, gia);
                MessageBox.Show("Đã cập nhật giá thành công");
            }
            _form.RefershListViewDSMonAn();
        }
    }
}
